(function ($) {

    $.extend({

        WIZ: {

            current: 1,

            init: function (opts, el) {

                var steps = $('#' + el.attr('id') + ' div[data-step]')

                if (opts) {

                    if (opts.onComplete) {
                        $.WIZ['done'] = opts.onComplete;
                    }

                }

                steps.each(function () {

                    if ($(this).attr('data-step') > 1) {
                        $(this).css('display', 'none');
                    }

                    var p = $(this);
                    var c = $(this).children();

                    c.each(function (i) {


                        if ($(this).attr('data-rel')) {

                            switch ($(this).attr('data-rel')) {

                                case 'next':

                                    $(this).on('click', function (event) {
                                        event.preventDefault();
                                        p.fadeOut(500, function () {
                                            $('div[data-step="' + ($.WIZ.current + 1) + '"]').fadeIn(500);
                                            $.WIZ.current++;

                                        });
                                    });

                                    break;

                                case 'back':

                                    $(this).on('click', function (event) {
                                        event.preventDefault();
                                        p.fadeOut(500, function () {
                                            $('div[data-step="' + ($.WIZ.current - 1) + '"]').fadeIn(500);
                                            $.WIZ.current--;
                                        });
                                    });

                                    break;

                                case 'done':

                                    if (!$.WIZ.done) {
                                        console.error('You must specify the "done" option if you want to use data-rel="done"');
                                    }
                                    else {
                                        $(this).on('click', function (event) {
                                            event.preventDefault();
                                            p.fadeOut(500, function () {
                                                $.WIZ.done();
                                            });
                                        });
                                    }

                                    break;

                                default:

                                    $(this).on('click', function (event) {
                                        event.preventDefault();

                                        var hop = parseInt($(this).attr('data-rel'));

                                        p.fadeOut(500, function () {

                                            $('div[data-step="' + hop + '"]').fadeIn(500);

                                            $.WIZ.current = hop;
                                        });
                                    });

                                    break;

                            }

                        }

                    });

                });
            }
        }
    });

    $.fn.wizard = function (opts) {

        if (opts) {
            $.WIZ.init(opts, $(this));
        }

        else {
            $.WIZ.init({}, $(this));
        }

    }

})(jQuery);